from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess,RegisterEventHandler,LogInfo
from launch.event_handlers import OnProcessExit,OnProcessStart

def generate_launch_description():
    t1=Node(package="turtlesim",executable="turtlesim_node")
    spawn=ExecuteProcess(
        cmd=["ros2 service call /spawn turtlesim/srv/Spawn \"{'x':8.0,'y':3.0,'theta':0.0}\""],
        output="both",
        shell=True
    )
    #
    event_start=RegisterEventHandler(
        event_handler=OnProcessStart(
            target_action=t1,
            on_start=spawn
        )
    )
    event_exit=RegisterEventHandler(
        event_handler=OnProcessExit(
            target_action=t1,
            on_exit=[LogInfo(msg="turtlesim_node 退出")]
        )
    )

    return LaunchDescription([t1,event_start,event_exit])